package user.internet;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import base.Constant;

/**
 * Created by wangic on 2017/2/8.
 */
public class Scan_utils {
    private static final String TAG = "Scan_utils";

    
    public void Scan(String code, final Handler handler){
        RequestParams requestParams = new RequestParams(Constant.scan_url);
        requestParams.addBodyParameter("code",code);
        Log.e(TAG, "Scan_utils: "+code );
        x.http().post(requestParams, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                Log.e(TAG, "onSuccess: "+result.toString() );
                Message message = new Message();
                try {
                    if(result.get("status").equals("100")){
                        message.what = 0;
                        message.obj = result.get("data");
                        handler.sendMessage(message);
                    }
                    else{
                        message.what = 1;
                        message.obj = result.get("message");
                        handler.sendMessage(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Message msg = new Message();
                msg.what = Constant.ERROR_MSG;
                handler.sendMessage(msg);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
